package com.micro.common.service.web.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.micro.common.service.orm.entity.DictDataPo;
import com.micro.common.service.orm.param.DictQueryParam;
import com.micro.common.service.orm.service.DictDataServiceImpl;
import com.micro.common.service.web.api.DictDataApi;
import com.micro.common.service.web.domain.req.IdReq;
import com.micro.common.service.web.domain.req.dict.DictAddReq;
import com.micro.common.service.web.domain.req.dict.DictEditReq;
import com.micro.common.service.web.domain.req.dict.DictPageListReq;
import com.micro.common.service.web.domain.resp.dict.DictDataResp;
import com.micro.common.service.web.domain.resp.dict.DictExcelResp;
import com.micro.framework.starter.common.data.enums.DeletedEnum;
import com.micro.framework.starter.common.data.req.PageReq;
import com.micro.framework.starter.common.data.result.PageResult;
import com.micro.framework.starter.common.data.result.Result;
import com.micro.framework.starter.excel.TemplateExcelUtils;
import com.micro.framework.starter.excel.dto.SheetData;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class DictDataApiService implements DictDataApi {
    @Resource
    private DictDataServiceImpl dictDataService;

    @Override
    public PageResult<DictDataResp> pageList(@Valid PageReq<DictPageListReq> req) {

        Page<DictDataPo> page = new Page<>(req.getPageIndex(), req.getPageSize());
        DictQueryParam param = BeanUtil.copyProperties(req.getData(), DictQueryParam.class);
        List<DictDataPo> list = dictDataService.getBaseMapper().dicPageList(page, param);
        if(CollectionUtils.isEmpty(list)){
            return PageResult.success(new ArrayList<>(), page);
        }
        List<DictDataResp> resList = BeanUtil.copyToList(list, DictDataResp.class);
        return PageResult.success(resList, page);
    }

    @Override
    public Result<Void> add(DictAddReq req) {
        DictDataPo dictDataPo = dictDataService.findByGroupAndCode(req.getGroupCode(),req.getDictCode());
        if(Objects.nonNull(dictDataPo)){
            return Result.fail("数据已存在");
        }
        dictDataPo = new DictDataPo();
        BeanUtil.copyProperties(req,dictDataPo);
        dictDataService.save(dictDataPo);
        return Result.success();
    }

    @Override
    public Result<Void> edit(@Valid DictEditReq req) {
        DictDataPo dictDataPo = dictDataService.getById(req.getId());
        if(Objects.isNull(dictDataPo)){
            return Result.fail("数据不存在");
        }
        DictDataPo dictCheck = dictDataService.findByGroupAndCode(req.getGroupCode(),req.getDictCode());
        if(Objects.nonNull(dictCheck) && !dictCheck.getId().equals(dictDataPo.getId())){
            return Result.fail("数据已存在");
        }
        BeanUtil.copyProperties(req,dictDataPo);
        dictDataService.updateById(dictDataPo);
        return Result.success();
    }

    @Override
    public Result<Void> delete(IdReq req) {
//        DictDataPo dictDataPo = dictDataService.getById(req.getId());
//        if(Objects.isNull(dictDataPo)){
//            return Result.fail("数据不存在");
//        }
//        dictDataPo.setDeleted(DeletedEnum.YES.getCode());
//        dictDataService.updateById(dictDataPo);
        dictDataService.getBaseMapper().test();

        return Result.success();
    }

    @Override
    public void dictExport(HttpServletResponse response) throws Exception {
        List<DictDataPo> poList = dictDataService.getBaseMapper().dicPageList(null,new DictQueryParam());
        List<DictExcelResp> respList = BeanUtil.copyToList(poList, DictExcelResp.class);
        SheetData sheetData = SheetData.builder().dataList(respList).sheetName("sheet0").headClass(DictExcelResp.class).build();
        SheetData sheetData2 = SheetData.builder().dataList(respList).sheetName("sheet1").lockPwd("ybx").headClass(DictExcelResp.class).build();
        SheetData sheetData3 = SheetData.builder().dataList(respList).sheetName("sheet2").headClass(DictExcelResp.class).build();
        TemplateExcelUtils.writerFile("字典", Lists.newArrayList(sheetData,sheetData2,sheetData3), response);
    }
}
